package mq

import (
	"charging_pile/devices/internal/svc"
	"charging_pile/util/protocol/jiuxing"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

var channel03 = make(chan *jiuxing.Package, 1000)

func handle03(nc *natsClient, in *jiuxing.Package) (jxMsg *jiuxing.Package) {
	// sn := jiuxing.GetBcdString(in.Sn)
	msg := jiuxing.Msg03{}
	msg.Decode(in.Payload)
	logx.Infof("msg03 %+v", msg)

	defer func() {
		resp := jiuxing.Msg0B{}
		resp.TimeOutCnt = byte(0)
		jxMsg = &jiuxing.Package{
			Cmd:        byte(0x0b),
			Sn:         in.Sn,
			Encryption: in.Encryption,
			PackageLen: 0,
			Payload:    []uint8{},
			Check:      0,
		}
		resp.Tm = formatJxTime()
		jxMsg.Payload = resp.Encode()
		jxMsg.PackageLen = uint16(len(jxMsg.Payload))
	}()

	// mode := nc.SvcCtx.Modes.ChargePileDev
	// rb := mode.RowBuilder().Where("sn=?", sn)
	// dev, err := mode.FindOneByQuery(context.Background(), rb)
	// if models.HasError(err) {
	// 	return nil
	// }

	// if dev == nil {
	// 	dev = &models.ChargeingPileDev{
	// 		Sn: sn,
	// 	}
	// }
	// dev.Tp = msg.Tp

	// if err == models.ErrNotFound {
	// 	mode.Insert(context.Background(), nil, dev)
	// } else {
	// 	mode.UpdateWithVersion(context.Background(), nil, dev)
	// }

	return
}

func beachHand03(nc *svc.ServiceContext) {
	for {
		select {
		case pack := <-channel03:
			print(pack)
		case <-time.After(500 * time.Millisecond):

		}
	}
}

func init() {
	HandlerMap[uint8(3)] = handle03
}
